const app = getApp();
import routerConfig from '../../page/router';
import routerInit from 'miniapp-router';
Component({
  mixins: [],
  data: {
    list: [
      {
        value: '1',
        label: '1次',
        disabled: false
      }, {
        value: '2',
        label: '2次'
      }
    ],
    list1: [
      {
        value: '10',
        label: '10秒',
        disabled: false
      }, {
        value: '20',
        label: '20秒'
      }, {
        value: '30',
        label: '30秒'
      }, {
        value: '60',
        label: '60秒'
      }, {
        value: '90',
        label: '90秒'
      }, {
        value: '0',
        label: '不限'
      }
    ],
    list2: [
      {
        value: '1',
        label: '店铺优惠券',
        disabled: false
      }, {
        value: '2',
        label: '商品优惠券',
        disabled: true
      }, {
        value: '3',
        label: '小礼物'
      }
    ],
    value: 1,
    value1: 1,
    value2: 1,
    goodsList: {},
    goodstitle: false,
    title: '',
    goodsUrl: '',
    selectTime: [app.callTime(1,0), app.callTime(2,7)],
    item: '1',
    user_item: '1',
    time: '1',
    remarks: '欢迎大家来提交视频参加活动! ',
    reward_type:'',
    // 参与奖励
    participate_rewards_type: '',
    participate_rewards_name: '',
    participate_rewards_id: '',
    // 通过奖励
    pass_rewards_type: '',
    pass_rewards_name: '',
    pass_rewards_id: '',
    // 上榜奖励
    on_rewards_type: '',
    on_rewards_name: '',
    on_rewards_id: '',
    // pass_rewards_type: false,
    // on_rewards_type: false,
    is_amoy: '',
    participate_rewards_type_show: false,
    pass_rewards_type_show: false,
    on_rewards_type_show: false,
    // participate_rewards_type_select: [],
    // pass_rewards_type_select: [],
    // on_rewards_type_select: [],
    participate:[], //参与奖励列表
    passData:[], //通过奖励列表
    onData:[], //上榜奖励列表
    
    shopData:[], //店铺优惠卷数据储存
    goodspData:[], //商品优惠卷数据储存
    giftData:[], //实物数据储存
    pagesize: 10,
  },
  props: {},
  didMount() {
    var app = getApp();
    routerInit.call(this, routerConfig);  //跳转页面初始化
    app.getUser();
    this.getShopCouponList();
    this.getKindList();
  },
  didUpdate() {
  },
  didUnmount() {
  },
  methods: {
    //创建活动
    handleSubmit() {
      if (!this.data.title) {
        my.alert({ content: '活动标题不能为空!' });
        return false;
      }
      if (!this.data.goodstitle) {
        my.alert({ content: '针对商品:请输您店铺内一个商品链接,' });
        return false;
      }
      if (this.data.time == 1) {
        my.alert({ content: '请选择视频长度' });
        return false;
      }
      if (this.data.is_amoy == true) {
        this.setData({ is_amoy: 1 });
      }else{
        this.setData({ is_amoy: 0 });
      }
      var apiurl = 'App.ShopRedsSales.Save';
      var params = {
        "title": this.data.title,
        "type": this.data.type,
        'start_time': this.data.selectTime[0],
        'stop_time': this.data.selectTime[1],
        "item": this.data.item,
        "user_item": this.data.user_item,
        "goods_platform_id": '1',
        "time": this.data.time,
        "goods_title": this.data.goodsList.title,
        "goods_outid": this.data.goodsList.outid,
        "goods_price": this.data.goodsList.price,
        "goods_pic": this.data.goodsList.pic_url,
        "remarks": this.data.remarks,
        "on_rewards_type": this.data.on_rewards_type,
        "on_rewards_name": this.data.on_rewards_name,
        "on_rewards_id": this.data.on_rewards_id,
        "pass_rewards_type": this.data.pass_rewards_type,
        "pass_rewards_name": this.data.pass_rewards_name,
        "pass_rewards_id": this.data.pass_rewards_id,
        "participate_rewards_type": this.data.participate_rewards_type,
        "participate_rewards_name": this.data.participate_rewards_name,
        "participate_rewards_id": this.data.participate_rewards_id,
        "is_amoy": this.data.is_amoy
      };
      console.log(params)
      var app = getApp();
      var self = this;
      my.confirm({
        title: '确认',
        content: '确定创建该活动吗?',
        success: (result) => {
          if (result.confirm) {
            app.xdapis(apiurl, params, function (e) {
              my.hideLoading();
              my.alert({ title: '活动创建成功!' });
              self.toActList();
            });
          }
        }
      });
      
       
    },
    //跳转到活动列表
    toActList(){
      var that = this;
      setTimeout(() => {
        let $router = that.$page.$router;
        $router.push(`/component/act-list`);
      }, 2000);
    },
    //监听选中的优惠卷
    redioAct(e){
      console.log(111111)
      console.log(e)
      var type = e.detail.value;
      let {
        detail: { value },
        currentTarget: { dataset }
      } = e;
      let { name } = dataset;
      var nameShow = name + "_show";
      this.setData({
        [name]: value,
        reward_type:type,
        [nameShow]: true
      }); 
       //选中当前按钮 
      if(name == "participate_rewards_type"){
         this.setData({participate: [],participate_rewards_name: '', participate_rewards_id: '',}); 
        if(type == 1){
           this.setData({ participate: this.data.shopData}); 
        }else{
           this.setData({participate: this.data.giftData}); 
        }  
      }else if(name == "pass_rewards_type"){
         this.setData({passData: [],pass_rewards_name: '',pass_rewards_id: '',}); 
        if(type == 1){
          this.setData({passData: this.data.shopData}); 
        }else{
          this.setData({passData: this.data.giftData}); 
        }  
      }else{
          this.setData({onData: [], on_rewards_name: '', on_rewards_id: '',}); 
        if(type == 1){
          this.setData({onData: this.data.shopData}); 
        }else{
          this.setData({onData: this.data.giftData}); 
        }
      }
      // console.log(this.data.participate)
      // my.alert({ content: JSON.stringify(this.data.participate) });
      // console.log(this.data.passData)
    },
    //参与奖励
    canJiang(e){
      var id = e.detail.value;
      console.log(id)
      var that = this;
      var res;
      if (that.data.reward_type == 1) {
        res = that.data.shopData;
      } else {
        res = that.data.giftData; 
      }
      for (let i = 0; i < res.length; i++) {
        if (id == res[i].value) {
          console.log(res[i].value)
          console.log(res[i].label)
          that.setData({
            participate_rewards_name: res[i].label,
            participate_rewards_id: res[i].value,
          }); 
        }
      }
    },
    //通过奖励
    tongJiang(e){
      var id = e.detail.value;
      console.log(id)
      var that = this;
      var res;
      if (that.data.reward_type == 1) {
        res = that.data.shopData;
      } else {
        res = that.data.giftData; 
      }
      for (let i = 0; i < res.length; i++) {
        if (id == res[i].value) {
          console.log(res[i].value)
          console.log(res[i].label)
          that.setData({
            pass_rewards_name: res[i].label,
            pass_rewards_id: res[i].value,
          }); 
        }
      }
    },
    //上榜奖励
    shangJiang(e){
      var id = e.detail.value;
      console.log(id)
      var that = this;
      var res;
      if (that.data.reward_type == 1) {
        res = that.data.shopData;
      } else {
        res = that.data.giftData; 
      }
      for (let i = 0; i < res.length; i++) {
        if (id == res[i].value) {
          console.log(res[i].value)
          console.log(res[i].label)
          that.setData({
            on_rewards_name: res[i].label,
            on_rewards_id: res[i].value,
          }); 
        }
      }
    },
    //监听事件
    onChange(e) {
      let {
        detail: { value },
        currentTarget: { dataset }
      } = e;
      let { name } = dataset;
      console.log(name + ' onChange:', value);
      this.setData({ [name]: value });
    },
    // onChange1(e) {
    //   console.log(e.detail.value)
    //   var selectData = e.currentTarget.dataset.name + "_select";
    //   console.log(selectData)
    //   if (e.detail.value == 1) {
    //     this.setData({
    //       selectData: []
    //     });
    //   }
      
    //   console.log(this.data.participate_rewards_type_select)
    //   let {
    //     detail: { value },
    //     currentTarget: { dataset }
    //   } = e;
    //   let { name } = dataset;
    //   console.log(name + ' onChange:', value);
    //   this.setData({ [name]: value });
    //   // 获取优惠券列表
    //   var nameShow = name + "_show";
    //   var dataselectName = name + "_select";
    //   console.log(dataselectName);
    //   //my.alert({ title: dataselectName });
    //   switch (value) {
    //     case '1':
    //       this.getShopCouponList(dataselectName);
    //       break;
    //     case '2':
    //       this.getGoodCouponList(dataselectName);
    //       break;
    //     case '3':
    //       this.getKindList(dataselectName);
    //       break;
    //   }

    //   this.setData({ [nameShow]: true });

    // },
    /** 获取店铺优惠券列表 */
    getShopCouponList() {
      var apiurl = 'App.ShopRedsInfos.GetUserCoupon';
      var params = {
        "page": 1,
        "pagesize": this.data.pagesize,
      };
      var app = getApp();
      var self = this;
      app.xdapis(apiurl, params, function (e) {
        my.hideLoading();
        var arr = [];
        for (let i = 0; i < e.list.length; i++) {
          arr.push({
            label: e.list[i].coupon_info.name,
            value: e.list[i].coupon_info.coupon_id
          });
        }
        self.setData({
          shopData: arr
        });
      });
    },
    /** 获取商品优惠卷 */
    getGoodCouponList(dataselectName) {

    },
    /** 获取实物礼品列表 */
    getKindList() {
      var apiurl = 'App.ShopRedsInfos.GetSimpleInKind';
      var params = {
        "page": 1,
        "pagesize": 10,
      };
      var app = getApp();
      var self = this;
      app.xdapis(apiurl, params, function (e) {
        my.hideLoading();
        var arr = [];
        for (let i = 0; i < e.list.length; i++) {
          arr.push({
            label: e.list[i].name,
            value: e.list[i].coupon_id
          });
        }
        self.setData({
          giftData: arr
        });
      });
    },
    handleParticipate() {

    },
    onChange2(e) {
      let {
        detail: { value },
        currentTarget: { dataset }
      } = e;
      let { name } = dataset;
      console.log(name + ' onChange:', value);
      this.setData({ [name]: value });
    },
    onChange3(e) {
      let {
        detail: { value },
        currentTarget: { dataset }
      } = e;
      let { name } = dataset;
      console.log(name + ' onChange:', value);
      this.setData({ [name]: value });
    },
    getGood(e){
      this.setData({
        goodsUrl: e.detail.value,
      });
      console.log(this.data.goodsUrl);
    },
    goTO(e) {
      console.log(111111111)
      if(!this.data.goodsUrl){
        my.alert({ content: '针对商品:请输入一个商品链接' });
        return false;
      }
      var apiurl = 'App.ShopRedsTools.UrlToGoods';
      var params = { "url": this.data.goodsUrl };
      var app = getApp();
      var self = this;
      app.xdapis(apiurl, params, function (e) {
        my.hideLoading();
        // my.alert({ title: e.title });
        self.setData({
          goodstitle: e.title,
          goodsList: e
        });
      });

    }
  },
});
